﻿using System;
using System.Text;
using System.IO;
using System.Web;

namespace MSL.Tool
{
    /// <summary>
    /// 
    /// </summary>
    public class JScript
    {
        #region 打开窗口
        /// <summary>
        /// 新开页面去掉ie的菜单
        /// </summary>
        /// <param name="url"></param>
        public static void OpenWindow(string url)
        {
            string js = @"<script language='javascript'>
			                    window.open('" + url + @"','','height=0,width=0,top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');
			             </script>";
            HttpContext.Current.Response.Write(js);
        }

        /// <summary>
        /// 打开窗口
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="name"></param>
        /// <param name="future"></param>
        public static void OpenWindow(string url, string name, string future)
        {
            string js = "<script language='javascript'>window.open('" + url + @"','" + name + @"','" + future + @"')</script>";
            HttpContext.Current.Response.Write(js);
        }

        /// <summary>
        /// 打开窗口
        /// </summary>
        /// <param name="url">地址</param>
        public static void OpenOneWindow(string url)
        {
            string js = "<script language='javascript'>window.open('" + url + @"','main');</script>";
            HttpContext.Current.Response.Write(js);
        }

        /// <summary>
        /// 在新页面打开
        /// </summary>
        /// <param name="url">地址</param>
        public static void OpenOneWindowBlank(string url)
        {
            string js = "<script language='javascript'>window.open('" + url + @"','_blank');</script>";
            HttpContext.Current.Response.Write(js);
        }

        /// <summary>
        /// 打开窗口
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="formName">窗口名称</param>
        public static void OpenWindow(string url, string formName)
        {
            string js = @"<script language='javascript'>
			                    window.open('" + url + @"','" + formName + @"','height=0,width=0,top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no','main');
			              </script>";

            HttpContext.Current.Response.Write(js);
        }

        /// <summary>
        /// 打开WEB窗口
        /// </summary>
        /// <param name="url">WEB窗口</param>
        /// <param name="isFullScreen">是否全屏幕</param>
        public static void OpenWindow(string url, bool isFullScreen, int intHeight, int intWidth, int intTop, int intLeft)
        {
            StringBuilder sbJS = new StringBuilder();
            sbJS.Append("<script language='javascript'>");
            if (isFullScreen)
            {
                sbJS.Append("var iWidth = 0;");
                sbJS.Append("var iHeight = 0;");
                sbJS.Append("iWidth=window.screen.availWidth-10;");
                sbJS.Append("iHeight=window.screen.availHeight-50;");
                sbJS.Append("var szFeatures ='width=' + iWidth + ',height=' + iHeight + ',top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no';");
                sbJS.Append("window.open('" + url + @"','',szFeatures);");
            }
            else
            {
                sbJS.Append("window.open('" + url + @"','','height=" + intHeight + ",width=" + intWidth + ",top=" + intTop + ",left=" + intLeft + ",location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no,directories=no');");
            }
            sbJS.Append("</script>");
            HttpContext.Current.Response.Write(sbJS.ToString());
        }
        #endregion

        #region 关闭窗体
        /// <summary>		
        /// 关闭窗口	
        /// </summary>
        public static void CloseParentWindow()
        {
            string js = "<script language='javascript'>window.parent.close();</script>";
            HttpContext.Current.Response.Write(js);
        }
        /// <summary>
        /// 关闭窗口
        /// </summary>
        public static void CloseOpenerWindow()
        {
            string js = "<script language='javascript'> window.opener.close(); </script>";
            HttpContext.Current.Response.Write(js);
        }
        /// <summary>
        /// 关闭窗体,没有任何提示的关闭窗体
        /// </summary>
        /// <param name="pageCurrent"></param>
        public static void CloseWindows(System.Web.UI.Page pageCurrent)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script>window.opener=null;window.close();</script>");
            pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(), System.Guid.NewGuid().ToString(), sb.ToString());
        }

        /// <summary>
        /// 有提示信息的关闭窗体
        /// </summary>
        /// <param name="pageCurrent"></param>
        /// <returns></returns>
        public static void CloseWindows(System.Web.UI.Page pageCurrent, string strMsg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script>if(confirm(\"" + strMsg + "\")==true){window.close();}</script>");
            pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(), System.Guid.NewGuid().ToString(), sb.ToString());
        }
        /// <summary>
        /// 有等待时间的关闭窗体
        /// </summary>
        /// <param name="pageCurrent"></param>
        /// <param name="WaitTime">等待时间，以毫秒为记量单位</param>
        public static void CloseWindows(System.Web.UI.Page pageCurrent, int WaitTime)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\">");
            //加入此行功能后没有提示功能
            sb.Append("window.opener=null;");
            sb.Append("setTimeout('window.close()'," + WaitTime.ToString() + ")");
            sb.Append("</script>");
            pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(), System.Guid.NewGuid().ToString(), sb.ToString());
        }
        #endregion

        #region 弹出对话框
        /// <summary>
        /// 弹出对话框(弹出对话框后css会失效)
        /// </summary>
        /// <param name="message">提示信息</param>
        public static void Alert(string strMsg)
        {
            HttpContext.Current.Response.Write("<script language='javascript'>window.alert('" + strMsg + "');</script>");
        }

        /// <summary>
        /// 显示一个弹出窗口，并转向目标页(导航)
        /// </summary>
        public static void AlertAndRedirect(string strMsg, string url)
        {
            System.Web.HttpContext.Current.Response.Write("<script language='javascript'>alert('" + strMsg.Trim() + "');window.location.href='" + url.Trim().Replace("'", "") + "';</script>");
        }

        /// <summary>
        /// 显示一个弹出窗口，重新加载当前页
        /// </summary>
        public static void AlertAndReLoad(string strMsg)
        {
            System.Web.HttpContext.Current.Response.Write("<script language='javascript'>alert('" + strMsg.Trim() + "');window.location.href=window.location.href;</script>");
        }

        /// <summary>
        /// 显示一个弹出窗口,刷新当前页(危险的,有可能陷入死循环)
        /// </summary>
        public static void AlertAndRefresh(string strMsg)
        {
            System.Web.HttpContext.Current.Response.Write("<script language='javascript'>alert('" + strMsg.Trim() + "');document.execCommand('Refresh');</script>");
        }

        /// <summary>
        /// 显示一个弹出窗口,并关闭当前页
        /// </summary>
        /// <param name="message"></param>
        public static void AlertAndClose(string strMsg)
        {
            System.Web.HttpContext.Current.Response.Write("<script language='javascript'>alert('" + strMsg.Trim() + "');window.close();</script>");
        }

        /// <summary>
        /// 显示一个弹出窗口,并转向上一页
        /// </summary>
        /// <param name="message"></param>
        public static void AlertAddPre(string strMsg)
        {
            System.Web.HttpContext.Current.Response.Write("<script language='javascript'>alert('" + strMsg.Trim() + "');window.location.href=document.referrer;</script>");
        }

        /// <summary>
        /// 显示一个弹出窗口,并转向上一页
        /// </summary>
        /// <param name="message"></param>
        public static void AlertGoHistory(string strMsg)
        {
            HttpContext.Current.Response.Write("<script language='javascript'>window.alert('" + strMsg + "');window.history.go(-1);return false;</script>");
        }

        #endregion

        #region 回到历史页面
        /// <summary>
        /// 回到历史页面
        /// </summary>
        /// <param name="value">-1/1,1表示前进，－1表示后退</param>
        public static void GoHistory(int value)
        {
            string js = "<script language='javascript'>history.go({0});</script>";
            HttpContext.Current.Response.Write(String.Format(js, value));
        }
        #endregion

        #region 返回父窗口
        /// <summary>		
        /// 返回父窗口	
        /// </summary>
        /// <param name="parentWindowUrl">父窗口</param>		
        public static void GotoParentWindow(string parentWindowUrl)
        {
            string js = "<script language='javascript'>this.parent.location.replace('" + parentWindowUrl + "');</script>";
            HttpContext.Current.Response.Write(js);
        }
        #endregion

        #region 替换父窗口
        /// <summary>		
        /// 替换父窗口	
        /// </summary>
        /// <param name="parentWindowUrl">父窗口</param>
        /// <param name="caption">窗口提示</param>
        /// <param name="future">窗口特征参数</param>
        public static void ReplaceParentWindow(string parentWindowUrl, string caption, string future)
        {
            string js = "";
            if (future != null && future.Trim() != "")
            {
                js = @"<script language=javascript>this.parent.location.replace('" + parentWindowUrl + "','" + caption + "','" + future + "');</script>";
            }
            else
            {
                js = @"<script language=javascript>var iWidth = 0 ;var iHeight = 0 ;iWidth=window.screen.availWidth-10;iHeight=window.screen.availHeight-50;
							var szFeatures = 'dialogWidth:'+iWidth+';dialogHeight:'+iHeight+';dialogLeft:0px;dialogTop:0px;center:yes;help=no;resizable:on;status:on;scroll=yes';this.parent.location.replace('" + parentWindowUrl + "','" + caption + "',szFeatures);</script>";
            }

            HttpContext.Current.Response.Write(js);
        }
        #endregion

        #region 替换当前窗体的打开窗口
        /// <summary>		
        /// 替换当前窗体的打开窗口	
        /// </summary>
        /// <param name="openerWindowUrl">当前窗体的打开窗口</param>		
        public static void ReplaceOpenerWindow(string openerWindowUrl)
        {
            string js = "<script language='javascript'>window.opener.location.replace('" + openerWindowUrl + "');</script>";
            HttpContext.Current.Response.Write(js);
        }
        #endregion

        #region 替换当前打开窗口的父窗口
        /// <summary>		
        /// 替换当前打开窗口的父窗口	
        /// </summary>
        ///<param name="frameWindowUrl"></param>
        /// <param name="frameName"></param>	
        public static void ReplaceOpenerParentFrame(string frameName, string frameWindowUrl)
        {
            string js = "<script language='javascript'>window.opener.parent." + frameName + ".location.replace('" + frameWindowUrl + "');</script>";
            HttpContext.Current.Response.Write(js);
        }
        #endregion

        #region 跳出带有框架的页面
        /// <summary>
        /// 跳出带有框架的页面
        /// </summary>
        /// <param name="pageCurrent">当前页面如this.page</param>
        /// <param name="strPage">要跳出的页面</param>
        public static void RedirectFrame(System.Web.UI.Page pageCurrent, string strPage)
        {
            pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(),
                    System.Guid.NewGuid().ToString(),
                    "<script>window.top.location.href='" + strPage + "'</script>"
                    );
        }
        #endregion

        #region 指定的框架页面转换
        /// <summary>
        /// 指定的框架页面转换
        /// </summary>
        /// <param name="FrameName">框架名称</param>
        /// <param name="url">URL</param>
        public static void javascriptFrameHref(string FrameName, string url)
        {
            string js = "<script language='javascript'>" + FrameName + ".location.replace(" + url + ");</script>";
            HttpContext.Current.Response.Write(js);
        }
        #endregion

        #region 重置框架页面
        /// <summary>
        /// 重置页面
        /// </summary>
        /// <param name="strRows"></param>
        public static void JScripttResetPage(string strRows)
        {
            string js = "<script language='javascript'>window.parent.CenterFrame.rows='" + strRows + "';</script>";
            HttpContext.Current.Response.Write(js);
        }
        #endregion

        #region 指向指定的某个页面
        /// <summary>
        /// 指向指定的某个页面
        /// </summary>
        /// <param name="strUrl">页面地址 如：index.htm</param>
        public static void Redirect(string strUrl)
        {
            string path = "http://" + System.Web.HttpContext.Current.Request.Url.Host + strUrl;
            HttpContext.Current.Response.Write("<script language='javascript'>window.parent.location.href('" + path + "');</script>");
        }
        #endregion

        #region 转向Url制定的页面
        /// <summary>
        /// 转向Url制定的页面
        /// </summary>
        /// <param name="url">Url</param>
        public static void JScriptLocationHref(string url)
        {
            string js = "<script language='javascript'>window.location.replace('" + url + "')</script>";
            HttpContext.Current.Response.Write(js);
        }
        #endregion

        #region 刷新页面
        public static void RefreshPage(System.Web.UI.Page pageCurrent)
        {
            string js = "<script language=\"javascript\"> window.location.reload(true); </script>";
            pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(), System.Guid.NewGuid().ToString(), js);
        }
        #endregion

        #region 刷新父窗口
        /// <summary>
        /// 刷新父窗口
        /// </summary>
        public static void RefreshParent()
        {
            string js = "<script language='javascript'>parent.location.reload();</script>";
            HttpContext.Current.Response.Write(js);
        }
        #endregion

        #region 给HTML表单中元素附值
        /// <summary>
        /// 给HTML表单中元素附值,如：SetHtmlElementValue("form1","txtName","cnmmmss")
        /// </summary>
        /// <param name="formName">表单名</param>
        /// <param name="elementName">标签namw</param>
        /// <param name="elementValue"></param>
        public static void SetHtmlElementValue(string formName, string elementName, string elementValue)
        {
            string js = @"<script language='javascript'>if(document." + formName + "." + elementName + "!=null){document." + formName + "." + elementName + ".value =" + elementValue + ";}</script>";
            HttpContext.Current.Response.Write(js);
        }
        #endregion

        #region 添加JS文件
        /// <summary>
        /// 添加JS文件
        /// </summary>
        /// <param name="page">页面</param>
        /// <param name="url">路径</param>
        public void AddScript(System.Web.UI.Page page, string url)
        {
            System.Web.UI.HtmlControls.HtmlGenericControl JsControl = new System.Web.UI.HtmlControls.HtmlGenericControl("script");
            JsControl.Attributes.Add("type", "text/javascript");
            JsControl.Attributes.Add("src", url);
            page.Header.Controls.Add(JsControl);
        }
        #endregion

        #region 添加CSS文件
        /// <summary>
        /// 添加CSS文件
        /// </summary>
        /// <param name="page"></param>
        /// <param name="url"></param>
        public void AddCss(System.Web.UI.Page page, string url)
        {
            System.Web.UI.HtmlControls.HtmlLink CssControl = new System.Web.UI.HtmlControls.HtmlLink();
            CssControl.Href = url;
            CssControl.Attributes.Add("rel", "stylesheet");
            CssControl.Attributes.Add("type", "text/css");
            page.Header.Controls.Add(CssControl);
        }
        #endregion

        #region 添加Meta标签
        /// <summary>
        /// 添加Meta标签
        /// </summary>
        /// <param name="page">页面</param>
        /// <param name="name">名</param>
        /// <param name="content">正文</param>
        public void AddMeta(System.Web.UI.Page page, string name, string content)
        {
            System.Web.UI.HtmlControls.HtmlMeta meta = new System.Web.UI.HtmlControls.HtmlMeta();
            meta.Name = name;
            meta.Content = content;
            page.Header.Controls.Add(meta);
        }
        #endregion
    }
}
